import { _decorator, Component, Node } from 'cc';
import { BottomNode } from './component/BottomNode';
import { KugoModel } from './KugoModel';
import { KugoControl } from './KugoControl';
import { EventMgr } from '../../../scripts/GlobalMessage';
import { KugoEvent } from './KugoConst';
import { DropChessReply, LoginRequest, WSCmd } from './proto/proto';
import { TopNode } from './component/TopNode';
import { netMgr } from '../../../scripts/net/NetMgr';
import { URLData } from '../../../scripts/URLData';
import { ChessPiecesMgr } from './component/chessbase/ChessPiecesMgr';
const { ccclass, property } = _decorator;

@ccclass('KugoScene')
export class KugoScene extends Component {
    @property(BottomNode)
    bottomBtnNode:BottomNode = null

    @property(TopNode)
    topNode:TopNode = null

    @property(ChessPiecesMgr)
    chessPiecesMgr:ChessPiecesMgr = null

    
    private _model:KugoModel = null
    private _control:KugoControl = null
    onLoad(): void {
        // DemoServer.getInstance().clear()
        this._control = KugoControl.getInstance()
        netMgr.initEvent("ws://127.0.0.1:1234/ws", URLData.userId, ()=>{
            console.log("链接成功")
            this._control.registerEvent()
            this._control.c2sLogin()
        })
        this._model = KugoModel.getInstance()
    }

    start(): void {
        this.showBottomNode()  

        EventMgr.on(KugoEvent.RefreshUser, this.refreshUser, this)
        EventMgr.on(KugoEvent.RefreshChess, this.refreshChess, this)
        EventMgr.on(KugoEvent.DropChessPieces, this.dropChessPieces, this)
        EventMgr.on(KugoEvent.OnClickBottom, this.onClickBottom, this)
    }

    /**显示下面按钮节点 */
    showBottomNode(){
        this.bottomBtnNode.show()
    }

    /**隐藏下面按钮节点 */
    hideBottomNode(){
        this.bottomBtnNode.hide()
    }

    /**刷新用户 */
    refreshUser(){
        this.topNode.refreshUser()
        EventMgr.send(KugoEvent.RefreshGameState)
    }

    /**刷新棋盘 */
    refreshChess(){
        this.chessPiecesMgr.refreshChess()
        EventMgr.send(KugoEvent.RefreshGameState)
    }

    /** */
    dropChessPieces(data:DropChessReply){

        this.chessPiecesMgr.dropCall(data) 
    }

    /** */
    onClickBottom(eventName:string){
        this.chessPiecesMgr.opt(eventName)
    }
}


